package com.chuanye.jiguangimdemo.activitys;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.chuanye.jiguangimdemo.R;
import com.chuanye.jiguangimdemo.app.MyApplication;
import com.chuanye.jiguangimdemo.entity.Event;
import com.chuanye.jiguangimdemo.entity.EventType;
import com.chuanye.jiguangimdemo.sharepreference.SharePreferenceManager;
import com.chuanye.jiguangimdemo.utils.NativeImageLoader;

import org.greenrobot.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.Date;

import cn.jpush.im.android.api.ContactManager;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.GetAvatarBitmapCallback;
import cn.jpush.im.android.api.callback.GetUserInfoCallback;
import cn.jpush.im.android.api.model.UserInfo;
import cn.jpush.im.api.BasicCallback;

/**
 * 好友验证时，查看好友详情页面
 */
public class SearchFriendDetailActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "SearchFriendDetailActivity";
    private Context mContext;


    private ImageView seafridetail_back_iv;
    private ImageView seafridetail_icon_iv;
    private TextView seafridetail_nickname_tv;
    private TextView seafridetail_signature_tv;//签名
    private TextView seafridetail_additionalmsg_tv;//附加消息
    private TextView seafridetail_username_tv;

    private TextView seafridetail_gender_tv;
    private TextView seafridetail_birthday_tv;
    private TextView seafridetail_address_tv;
    private Button seafridetail_refusal_btn;//拒绝按钮
    private Button seafridetail_agree_btn;//同意按钮

    private String mUsername;
    private String mAppKey;
    private UserInfo mToUserInfo;

    private String mAvatarPath;
    private String mDisplayName;
    private int position;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_friend_detail);
        mContext = this;

        initView();
    }

    private void initView() {
        //返回
        seafridetail_back_iv = findViewById(R.id.seafridetail_back_iv);
        seafridetail_back_iv.setOnClickListener(this);
        //用户头像
        seafridetail_icon_iv = findViewById(R.id.seafridetail_icon_iv);
        //头像下的昵称 或备注名
        seafridetail_nickname_tv = findViewById(R.id.seafridetail_nickname_tv);
        //签名
        seafridetail_signature_tv = findViewById(R.id.seafridetail_signature_tv);
        //附加消息
        seafridetail_additionalmsg_tv = findViewById(R.id.seafridetail_additionalmsg_tv);
        //用户名
        seafridetail_username_tv = findViewById(R.id.seafridetail_username_tv);
        //性别
        seafridetail_gender_tv = findViewById(R.id.seafridetail_gender_tv);
        //生日
        seafridetail_birthday_tv = findViewById(R.id.seafridetail_birthday_tv);
        //地区
        seafridetail_address_tv = findViewById(R.id.seafridetail_address_tv);

        //拒绝按钮
        seafridetail_refusal_btn = findViewById(R.id.seafridetail_refusal_btn);
        seafridetail_refusal_btn.setOnClickListener(this);

        //同意按钮
        seafridetail_agree_btn = findViewById(R.id.seafridetail_refusal_btn);
        seafridetail_agree_btn.setOnClickListener(this);


        initData();

    }

    private void initData() {
        final Intent intent = getIntent();
        mUsername = intent.getStringExtra(MyApplication.TARGET_ID);
        mAppKey = intent.getStringExtra(MyApplication.TARGET_APP_KEY);
        position = intent.getIntExtra("position", -1);

        JMessageClient.getUserInfo(mUsername, mAppKey, new GetUserInfoCallback() {
            @Override
            public void gotResult(int responseCode, String responseMessage, UserInfo info) {
                if (responseCode == 0) {
                    mToUserInfo = info;
                    Bitmap bitmap = NativeImageLoader.getInstance().getBitmapFromMemCache(mUsername);
                    if (bitmap != null) {
                        seafridetail_icon_iv.setImageBitmap(bitmap);
                    } else if (!TextUtils.isEmpty(info.getAvatar())) {
                        mAvatarPath = info.getAvatarFile().getPath();
                        info.getAvatarBitmap(new GetAvatarBitmapCallback() {
                            @Override
                            public void gotResult(int responseCode, String responseMessage, Bitmap avatarBitmap) {
                                if (responseCode == 0) {
                                    seafridetail_icon_iv.setImageBitmap(avatarBitmap);
                                    NativeImageLoader.getInstance().updateBitmapFromCache(mUsername, avatarBitmap);
                                } else {
                                    seafridetail_icon_iv.setImageResource(R.drawable.rc_default_portrait);
                                }
                            }
                        });
                    } else {
                        seafridetail_icon_iv.setImageResource(R.drawable.rc_default_portrait);
                    }
                    mDisplayName = info.getNickname();
                    if (TextUtils.isEmpty(mDisplayName)) {
                        mDisplayName = info.getUserName();
                    }
                    seafridetail_nickname_tv.setText(mDisplayName);
                    if (info.getGender() == UserInfo.Gender.male) {
                        seafridetail_gender_tv.setText("男");
                    } else if (info.getGender() == UserInfo.Gender.female) {
                        seafridetail_gender_tv.setText("女");
                    } else {
                        seafridetail_gender_tv.setText("保密");
                    }
                    //附加消息
                    seafridetail_additionalmsg_tv.setText("附加消息: " + intent.getStringExtra("reason"));
                    //签名
                    seafridetail_signature_tv.setText(info.getSignature());
                    //用户名
                    seafridetail_username_tv.setText(mUsername);
                    //生日
                    long birthday = info.getBirthday();
                    if (birthday == 0) {
                        seafridetail_birthday_tv.setText("");
                    } else {
                        Date date = new Date(birthday);
                        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        seafridetail_birthday_tv.setText(dateFormat.format(date));
                    }
                    //地区
                    seafridetail_address_tv.setText(info.getRegion());
                }
            }
        });



    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.seafridetail_back_iv://返回
                finish();
                break;
            case R.id.seafridetail_refusal_btn://拒绝按钮
                //拒绝
                ContactManager.declineInvitation(mToUserInfo.getUserName(), mToUserInfo.getAppKey(), "拒绝了您的好友请求", new BasicCallback() {
                    @Override
                    public void gotResult(int responseCode, String responseMessage) {
                        if (responseCode == 0) {
                            //拒绝时候要修改button数据库状态,并更新内存
                            Intent btnIntent = new Intent();
                            btnIntent.putExtra("position", position);
                            btnIntent.putExtra("btn_state", 1);
                            setResult(MyApplication.RESULT_BUTTON, btnIntent);
                            finish();
                        }
                    }
                });
                break;
            case R.id.seafridetail_agree_btn://同意按钮
                ContactManager.acceptInvitation(mToUserInfo.getUserName(), mToUserInfo.getAppKey(), new BasicCallback() {
                    @Override
                    public void gotResult(int responseCode, String responseMessage) {
                        if (responseCode == 0) {
                            Intent btnIntent2 = new Intent();
                            btnIntent2.putExtra("position", position);
                            btnIntent2.putExtra("btn_state", 2);
                            setResult(MyApplication.RESULT_BUTTON, btnIntent2);
                            //添加好友
                            EventBus.getDefault().post(new Event.Builder().setType(EventType.addFriend)
                                    .setFriendId(SharePreferenceManager.getItem()).build());
                            finish();
                        }
                    }
                });
                break;
        }
    }
}
